/**
 * SearchProperties.java
 */
package com.mht2017.common.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author yutong.xiao
 * @date 2018年1月9日 上午10:53:58
 */
public class SearchProperties extends Properties {

	private static final long serialVersionUID = 4318051216846565284L;
	private static final Logger LOGGER = LoggerFactory.getLogger(SearchProperties.class);
	private static final String actionName = "搜索配置";

	@Override
	public String getProperty(String key) {
		String fromCommandLine = System.getProperty(key);
		String fromConf = super.getProperty(key);
		if (null == fromCommandLine) {
			return fromConf;
		} else {
			LOGGER.info(String.format("%s-从系统参数覆盖配置。'%s'='%s'覆盖为'%s'", actionName, key, fromConf, fromCommandLine));
			return fromCommandLine;
		}
	}

	@Override
	public String getProperty(String key, String defaultValue) {
		String fromCommandLine = System.getProperty(key);
		String fromConf = super.getProperty(key, defaultValue);
		if (null == fromCommandLine) {
			return fromConf;
		} else {
			LOGGER.info(String.format("%s-从系统参数覆盖配置。'%s'='%s'覆盖为'%s'", actionName, key, fromConf, fromCommandLine));
			return fromCommandLine;
		}
	}

	public long getLong(String key, long defaultValue) {
		String property = getProperty(key);
		try {
			return Long.parseLong(property);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public int getInt(String key, int defaultValue) {
		String property = getProperty(key);
		try {
			return Integer.parseInt(property);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

}
